package cn.hospital.seviceimpl;

import cn.hospital.dao.*;
import cn.hospital.entity.*;
import cn.hospital.service.ClinicalDataQueryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ClinicalDataQueryServiceImpl implements ClinicalDataQueryService {
    @Resource
    private ClinicalDataQueryMapper clinicalDataQueryMapper;
    @Resource
    private QqueryConditionsMapper qqueryConditionsMapper;
    @Resource
    private DepartmentMapper departmentMapper;
    @Resource
    private QueryAuditManagementMapper queryAuditManagementMapper;
    @Resource
    private MeaningUserMapper meaningUserMapper;

    /**
     * 根据结束时间查询有效的审核数据
     * @param map map集合（结束时间，页码，每页的条数）
     * @return
     */
    public List<ClinicalDataQuery> findByEffective(Map<String, Object> map) {
        return clinicalDataQueryMapper.findByEffective(map);
    }

    /**
     * 根据结束时间查询有效的正在审批数据的数量
     * @param map 集合（结束时间）
     * @return
     */
    public Long findByCountEffectiveInExamination(Map<String, Object> map) {
        return clinicalDataQueryMapper.findByCountEffectiveInExamination(map);
    }

    /**
     * 根据结束时间查询有效的审批数据的数量
     * @param map 集合（结束时间）
     * @return
     */
    public Long findByCountEffective(Map<String, Object> map) {
        return clinicalDataQueryMapper.findByCountEffective(map);
    }

    /**
     * 根据结束时间查询无效的审核数据
     * @param map map集合（结束时间，页码，每页的条数）
     * @return
     */
    public List<ClinicalDataQuery> findByInvalid(Map<String, Object> map) {
        return clinicalDataQueryMapper.findByInvalid(map);
    }

    /**
     * 根据结束时间查询无效的审批数据的数量
     * @param map 集合（结束时间）
     * @return
     */
    public Long findByCountInvalid(Map<String, Object> map) {
        return clinicalDataQueryMapper.findByCountInvalid(map);
    }

    /**
     * 添加查询数据按钮进行最后存放
     * @param session
     * @return
     */
    public Integer addBy(HttpSession session) {
        Integer count = 0;
        ClinicalDataQuery clinicalDataQuery = (ClinicalDataQuery) session.getAttribute("clinicalDataQuery");
        String[] query_field = (String[]) session.getAttribute("query_field");
        String[] query = (String[]) session.getAttribute("query");
        String[] query_conditions = (String[]) session.getAttribute("query_conditions");
        String[] scope_of = (String[]) session.getAttribute("scope_of");
        //完善实体类中数据-queryConditions
        QqueryConditions queryConditions = new QqueryConditions();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        Date date = new Date();
        MeaningUser meaningUser = (MeaningUser) session.getAttribute("meaningUser");
        String ApplicationInformationId = sdf.format(date) + meaningUser.getUsername();
        queryConditions.setApplicationInformationId(ApplicationInformationId);
        if (query_field != null) {
            for (int i = 0; i < query_field.length; i++) {
                queryConditions.setQueryField(query_field[i]);
                queryConditions.setQuery(query[i]);
                queryConditions.setQueryConditions(query_conditions[i]);
                queryConditions.setScopeOf(scope_of[i]);
                count = insertQueryConditions(queryConditions);
            }
        }
        //完善实体类中数据-clinicalDataQuery
        clinicalDataQuery.setMeaningUserId(meaningUser.getUserId());
        clinicalDataQuery.setQueryConditionsId(ApplicationInformationId);
        count = insertClinicalDataQuery(clinicalDataQuery);
        addQueryAuditManagement(ApplicationInformationId);
        return count;
    }

    /**
     * 删除查询方法，根据查询信息id，删除对应查询信息
     * @param query_conditions_id
     * @return
     */
    public Integer deleteBy(String query_conditions_id) {
        Integer count = 0;
        count = deleteByApplication_information_id(query_conditions_id);
        count = deleteByQuery_conditions_id(query_conditions_id);
        count = deleteByClinicalDataQueryId(query_conditions_id);
        return count;
    }

    /**
     * 根据查询信息id，查询该条查询信息
     * @param queryConditionsId
     * @return
     */
    public ClinicalDataQuery updateByQueryConditionsId(String queryConditionsId) {
        return clinicalDataQueryMapper.updateByQueryConditionsId(queryConditionsId);
    }

    /**
     * 根据页面id，查询条件信息
     * @param applicationInformationId
     * @return
     */
    public List<QqueryConditions> selectByApplicationInformationId(String applicationInformationId) {
        return qqueryConditionsMapper.selectByApplicationInformationId(applicationInformationId);
    }

    /**
     * 修改有效时间，根据查询信息id修改有效时间
     * @param queryConditionsId
     * @return
     */
    public Integer updateByEndTime(String queryConditionsId, String endtime) {
        return clinicalDataQueryMapper.updateByEndTime(queryConditionsId, endtime);
    }


    /**------------------------------------------（审核管理）------------------------------------------*/

    /**
     * 根据查询id，查询审核进度
     *
     * @param clinicalDataQueryId
     * @return
     */
    public List<QueryAuditManagement> selectByClinicalDataQueryId(String clinicalDataQueryId) {
        Integer queryAuditManagementState = 3;
        return queryAuditManagementMapper.selectByClinicalDataQueryId(clinicalDataQueryId, queryAuditManagementState);
    }

    /**
     * 查询用户（医生）的信息（id，姓名）
     *
     * @return
     */
    public List<MeaningUser> selectMeaningUserBy() {
        return meaningUserMapper.selectBy();
    }

    /**
     * 有效的审核中待审批
     * @param session
     * @return
     */
    public List<ClinicalDataQuery> selectByAuditEffective(HttpSession session, Map<String, Object> map) {
        MeaningUser meaningUser = (MeaningUser) session.getAttribute("meaningUser");
        Department department = departmentMapper.selectByDepartmentId(meaningUser.getDepartmentId());
        map.put("queryAuditManagementState", 3);
        map.put("queryAuditManagementDepartment", department.getDepartmentName());
        map.put("time", new Date());
        return clinicalDataQueryMapper.selectByAuditEffective(map);
    }

    /**
     * 有效的审核中待审批的数量
     * @param session
     * @return
     */
    public Long selectByAuditEffectiveCount(HttpSession session, Map<String, Object> map) {
        MeaningUser meaningUser = (MeaningUser) session.getAttribute("meaningUser");
        Department department = departmentMapper.selectByDepartmentId(meaningUser.getDepartmentId());
        map.put("queryAuditManagementState", 3);
        map.put("queryAuditManagementDepartment", department.getDepartmentName());
        map.put("time", new Date());
        return clinicalDataQueryMapper.selectByAuditEffectiveCount(map);
    }

    /**
     * 有效的审核中审批通过
     *
     * @param session
     * @return
     */
    public List<ClinicalDataQuery> selectByApprovedEffective(HttpSession session, Map<String, Object> map) {
        MeaningUser meaningUser = (MeaningUser) session.getAttribute("meaningUser");
        Department department = departmentMapper.selectByDepartmentId(meaningUser.getDepartmentId());
        map.put("queryAuditManagementState", 1);
        map.put("queryAuditManagementDepartment", department.getDepartmentName());
        map.put("time", new Date());
        return clinicalDataQueryMapper.selectByAuditEffective(map);
    }

    /**
     * 有效的审核中审批通过的数量
     *
     * @param session
     * @return
     */
    public Long selectByApprovedEffectiveCount(HttpSession session, Map<String, Object> map) {
        MeaningUser meaningUser = (MeaningUser) session.getAttribute("meaningUser");
        Department department = departmentMapper.selectByDepartmentId(meaningUser.getDepartmentId());
        map.put("queryAuditManagementState", 1);
        map.put("queryAuditManagementDepartment", department.getDepartmentName());
        map.put("time", new Date());
        return clinicalDataQueryMapper.selectByAuditEffectiveCount(map);
    }

    /**
     * 有效的审核中审批未通过
     *
     * @param session
     * @return
     */
    public List<ClinicalDataQuery> selectByFailedPassAuditEffective(HttpSession session, Map<String, Object> map) {
        MeaningUser meaningUser = (MeaningUser) session.getAttribute("meaningUser");
        Department department = departmentMapper.selectByDepartmentId(meaningUser.getDepartmentId());
        map.put("queryAuditManagementState", 2);
        map.put("queryAuditManagementDepartment", department.getDepartmentName());
        map.put("time", new Date());
        return clinicalDataQueryMapper.selectByAuditEffective(map);
    }

    /**
     * 有效的审核中审批未通过的数量
     *
     * @param session
     * @return
     */
    public Long selectByFailedPassAuditEffectiveCount(HttpSession session, Map<String, Object> map) {
        MeaningUser meaningUser = (MeaningUser) session.getAttribute("meaningUser");
        Department department = departmentMapper.selectByDepartmentId(meaningUser.getDepartmentId());
        map.put("queryAuditManagementState", 2);
        map.put("queryAuditManagementDepartment", department.getDepartmentName());
        map.put("time", new Date());
        return clinicalDataQueryMapper.selectByAuditEffectiveCount(map);
    }

    /**
     * 修改审核状态
     * @param queryAuditManagement
     * @return
     */
    public Integer updateByqueryAuditManagementId(QueryAuditManagement queryAuditManagement) {
        ClinicalDataQuery clinicalDataQuery = new ClinicalDataQuery();
        clinicalDataQuery.setQueryConditionsId(queryAuditManagement.getClinicalDataQueryId());
        Integer count = updateStateByqueryConditionsId(clinicalDataQuery);
        return queryAuditManagementMapper.updateByQueryAuditManagementId(queryAuditManagement);
    }


    /*-----------------------------------(本实现类，自带方法)--------------------------------------*/

    /**
     * 添加需要审核的查询信息
     * @param clinicalDataQuery 查询实体类
     * @return
     */
    public Integer insertClinicalDataQuery(ClinicalDataQuery clinicalDataQuery) {
        clinicalDataQuery.setStateTime(new Date());
        clinicalDataQuery.setApproverState("2");
        clinicalDataQuery.setConfidentialityAgreementState("1");
        return clinicalDataQueryMapper.insertBy(clinicalDataQuery);
    }

    /**
     * 添加查询信息，审核管理查询信息
     * @param ApplicationInformationId
     */
    public void addQueryAuditManagement(String ApplicationInformationId) {
        QueryAuditManagement queryAuditManagement = new QueryAuditManagement();
        queryAuditManagement.setQueryAuditManagementState(3);
        queryAuditManagement.setClinicalDataQueryId(ApplicationInformationId);
        for (int i = 0; i < 3; i++) {
            if (i == 0) {
                queryAuditManagement.setQueryAuditManagementDepartment("信息科");
            } else if (i == 1) {
                queryAuditManagement.setQueryAuditManagementDepartment("检验科");
            } else if (i == 2) {
                queryAuditManagement.setQueryAuditManagementDepartment("化验科");
            }
            queryAuditManagementMapper.addByClinicalDataQueryId(queryAuditManagement);
        }
    }

    /**
     * 添加查询条件
     * @param queryConditions
     * @return
     */
    public Integer insertQueryConditions(QqueryConditions queryConditions) {
        return qqueryConditionsMapper.insert(queryConditions);
    }

    /**
     * 删除方法，根据查询信息id，删除对应查询条件
     * @param application_information_id
     * @return
     */
    public Integer deleteByApplication_information_id(String application_information_id) {
        return qqueryConditionsMapper.deleteByApplicationInformationId(application_information_id);
    }

    /**
     * 删除方法，根据查询信息id删除查询信息
     * @param query_conditions_id
     * @return
     */
    public Integer deleteByQuery_conditions_id(String query_conditions_id) {
        return clinicalDataQueryMapper.deleteByQueryConditionsId(query_conditions_id);
    }

    /**
     * 根据查询信息id，删除查询审核信息
     *
     * @param clinicalDataQueryId
     * @return
     */
    public Integer deleteByClinicalDataQueryId(String clinicalDataQueryId) {
        return queryAuditManagementMapper.deleteByClinicalDataQueryId(clinicalDataQueryId);
    }

    /**
     * 根据查询条件id，修改查询信息的状态
     *
     * @param clinicalDataQuery
     * @return
     */
    public Integer updateStateByqueryConditionsId(ClinicalDataQuery clinicalDataQuery) {
        clinicalDataQuery.setApproverState("1");
        return clinicalDataQueryMapper.updateStateByqueryConditionsId(clinicalDataQuery);
    }
}
